package my.learnDB.mybatis.daoAnno;

import my.learnDB.mybatis.domain.Account;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 注解开发：详见Dao配置
 * "@Insert"
 * "@Update"
 * "@Delete"
 * "@Select"
 * <p>
 * "@Result"	// 数据库的字段 和 实体类的属性进行映射
 * "@Results"   // 包含多个 Result
 * "@ResultMap"    // 复用 Results
 * "@One"        // 一对一结果集封装
 * "@Many"        // 一对多结果集封装
 * "@SelectProvider"        // 实现动态 SQL 映射
 * "@CacheNamespace"        // 实现注解二级缓存的使用"
 * private Integer id;
 * private String accountName;  // 数据库 name
 * private Integer uid;  // 数据库 userId
 * private Float money;
 * private Date birthday;
 * private String address;
 */

@CacheNamespace(blocking=true)
public interface AccountDao {


    // findById
    @ResultMap(value = "accountMap")
    @Select("select * from account where id = #{id}")
    Account findById(Integer id);

    // findByUid
    @ResultMap(value = "accountMap")
    @Select("select * from account where userId = #{userId}")
    List<Account> findByUid(Integer userId);

    // findAll
    @Select("select * from account")
    @Results(id = "accountMap",  // 结果集id，便于复用
            value = {
                    @Result(id = true, column = "id", property = "id"),  // column: 数据库字段， property：实体类属性
                    @Result(column = "name", property = "accountName"),
                    @Result(column = "userId", property = "uid"),
                    @Result(column = "money", property = "money"),
                    @Result(column = "birthday", property = "birthday"),
                    @Result(column = "address", property = "address")
            })
    List<Account> findAll();


    // ********************关系******************************************

    // 多对一：一对一
    @Select("select * from account a, user u where a.userId = u.id")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),  // column: 数据库字段， property：实体类属性
            @Result(column = "name", property = "accountName"),
            @Result(column = "userId", property = "uid"),
            @Result(column = "money", property = "money"),
            @Result(column = "birthday", property = "birthday"),
            @Result(column = "address", property = "address"),
            @Result(column = "uid", property = "user",
                    one = @One(select = "my.learnDB.mybatis.daoAnno.UserDao.findById"))
    })
    List<Account> relationFindAll();

}